import { reqAddHospital, reqUpdateHospital } from '@/api/hospital/hospitalSet'
import { AddHospitalParams } from '@/api/hospital/model/hospitalSetTypes'
import { Button, Card, Form, message } from 'antd'
import { useForm } from 'antd/lib/form/Form'
import Input from 'antd/lib/input/Input'
import React, { useEffect } from 'react'
import { useLocation, useNavigate } from 'react-router-dom'

export default function AddOrUpdateHospital() {
  const navigate = useNavigate()
  const [form] = useForm()
  const loaction: any = useLocation();

  useEffect(() => {
    if (loaction.state) {
      form.setFieldsValue(loaction.state)
    }
  }, [])

  const goBack = () => {
    navigate("/syt/hospital/hospitalSet")
  }

  //保存
  const onFinish = async (values: AddHospitalParams) => {
    if (loaction.state) {
      await reqUpdateHospital({
        ...values, // 将values数据展开到新对象中
        id: loaction.state.id, // 添加id数据
      });
      message.success("编辑医院成功!")
    } else {
      await reqAddHospital(values)
      message.success("添加医院成功!")
    }

    goBack()
  }


  return (
    <Card>
      <Form labelCol={{ span: 2 }} wrapperCol={{ span: 22 }} onFinish={onFinish} form={form}>
        <Form.Item label="医院名称" name="hosname" rules={[{ required: true, message: "请输入医院名称!" }]}>
          <Input></Input>
        </Form.Item>
        <Form.Item label="医院编号" name="hoscode" rules={[{ required: true, message: "请输入医院编号" }]}>
          <Input></Input>
        </Form.Item>
        <Form.Item label="api基础路径" name="apiUrl" rules={[{ required: true, message: "请输入api基础路径!" }]}>
          <Input></Input>
        </Form.Item>
        <Form.Item label="联系人姓名" name="contactsName" rules={[{ required: true, message: "请输入联系人姓名!" }]}>
          <Input></Input>
        </Form.Item>
        <Form.Item label="联系人手机" name="contactsPhone" rules={[{ required: true, message: "请输入联系人手机!" }]}>
          <Input></Input>
        </Form.Item>
        <Form.Item wrapperCol={{ offset: 2 }}>
          <Button type="primary" htmlType="submit">
            保存
          </Button>
          <Button className="ml" onClick={goBack}>
            返回
          </Button>
        </Form.Item>
      </Form>
    </Card>
  )
}
